package com.yunhe.marketing.util;

import com.yunhe.common.util.StringUtil;
import com.yunhe.marketing.constant.EnterpriseConsts;
import com.yunhe.marketing.domain.Enterprise;
import com.yunhe.marketing.model.EnterpriseUpload;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

/**
 * @Author: DongWenYu
 * @Date: 2021/2/26 14:50
 */
public class EnterpriseUploadUtil {

    public static Boolean isEmpty(EnterpriseUpload enterpriseUpload){
        if (StringUtil.isEmpty(enterpriseUpload.getEnterpriseName()) && StringUtil.isEmpty(enterpriseUpload.getOperationStatusTitle())
                && StringUtil.isEmpty(enterpriseUpload.getIndustryCategoryTitle())&& StringUtil.isEmpty(enterpriseUpload.getLegalRepresentative())
                && StringUtil.isEmpty(enterpriseUpload.getContacts())&& StringUtil.isEmpty(enterpriseUpload.getContactNumber())
                && StringUtil.isEmpty(enterpriseUpload.getOtherNumber())&& StringUtil.isEmpty(enterpriseUpload.getRegisteredCapital())
                && StringUtil.isEmpty(enterpriseUpload.getPaidInCapital())&& StringUtil.isEmpty(enterpriseUpload.getEstablishDate())
                && StringUtil.isEmpty(enterpriseUpload.getProvinceTitle())&& StringUtil.isEmpty(enterpriseUpload.getCityTitle())
                && StringUtil.isEmpty(enterpriseUpload.getDistrictTitle())&& StringUtil.isEmpty(enterpriseUpload.getNumberOfInsured())
                && StringUtil.isEmpty(enterpriseUpload.getEnterpriseScale())&& StringUtil.isEmpty(enterpriseUpload.getEnterpriseTypeTitle())
                && StringUtil.isEmpty(enterpriseUpload.getAnnualReportTitle())&& StringUtil.isEmpty(enterpriseUpload.getLicense())
                && StringUtil.isEmpty(enterpriseUpload.getRegisterAddress())&& StringUtil.isEmpty(enterpriseUpload.getEmail())
                && StringUtil.isEmpty(enterpriseUpload.getOtherEmail())&& StringUtil.isEmpty(enterpriseUpload.getBusinessNature())
                && StringUtil.isEmpty(enterpriseUpload.getHighTechEnterpriseTitle())&& StringUtil.isEmpty(enterpriseUpload.getSelfResearchTeamTitle())
                && StringUtil.isEmpty(enterpriseUpload.getUnicornEnterpriseTitle())&& StringUtil.isEmpty(enterpriseUpload.getChinaTop500Title())
                && StringUtil.isEmpty(enterpriseUpload.getWorldTop500Title())&& StringUtil.isEmpty(enterpriseUpload.getMicroEnterpriseTitle())
                && StringUtil.isEmpty(enterpriseUpload.getAnnualReportTitle())&& StringUtil.isEmpty(enterpriseUpload.getBusinessModifyTitle())
                && StringUtil.isEmpty(enterpriseUpload.getBranchTitle())&& StringUtil.isEmpty(enterpriseUpload.getRecruitmentInformationTitle())
                && StringUtil.isEmpty(enterpriseUpload.getWebsiteInformationTitle())&& StringUtil.isEmpty(enterpriseUpload.getWebsite())
                && StringUtil.isEmpty(enterpriseUpload.getIntentionTitle())&& StringUtil.isEmpty(enterpriseUpload.getIntentionArea())
                && StringUtil.isEmpty(enterpriseUpload.getIntentionPark())&& StringUtil.isEmpty(enterpriseUpload.getPurposeTitle())
                && StringUtil.isEmpty(enterpriseUpload.getAreaRequirement())&& StringUtil.isEmpty(enterpriseUpload.getFloorHeight())
                && StringUtil.isEmpty(enterpriseUpload.getBearing())&& StringUtil.isEmpty(enterpriseUpload.getElectricityConsumption())
                && StringUtil.isEmpty(enterpriseUpload.getMatchingRequirementTitle())&& StringUtil.isEmpty(enterpriseUpload.getOther())){
            return true;
        }
        return false;
    }


    public static void update(EnterpriseUpload target , EnterpriseUpload source){
        Class thisClass = target.getClass();
        Class enterpriseUploadClass = source.getClass();

        Field[] thisFields = thisClass.getDeclaredFields();
        Field[] enterpriseUploadFields = enterpriseUploadClass.getDeclaredFields();
        for(int i=0; i<enterpriseUploadFields.length; i++){
            Field enterpriseUploadField = enterpriseUploadFields[i];
            if(Modifier.isStatic(enterpriseUploadField.getModifiers())){
                continue;
            }
            Field thisField = thisFields[i];
            if(Modifier.isStatic(thisField.getModifiers())){
                continue;
            }
            enterpriseUploadField.setAccessible(true);
            thisField.setAccessible(true);
            try {
                if( !isNull(enterpriseUploadField,source)) {
                    if (isNull(thisField,target)) {
                        thisField.set(target, enterpriseUploadField.get(source));
                    }
                }
            } catch (IllegalArgumentException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
    private static Boolean isNull(Field enterpriseUploadField , EnterpriseUpload enterpriseUpload) throws IllegalAccessException {
        if( (enterpriseUploadField.get(enterpriseUpload) == null &&  !"serialVersionUID".equals(enterpriseUploadField.getName().toString()))
                || (StringUtil.isEmpty(enterpriseUploadField.get(enterpriseUpload).toString())  &&  !"serialVersionUID".equals(enterpriseUploadField.getName().toString()))){
            return true;
        }
        else return false;
    }
}
